﻿using System;
using System.Reflection;
using System.Windows.Forms;

namespace QL_Truong_Hoc.DanhMuc
{
    public partial class frmDanhSachDuTuyen : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "tuyen_sinh";

        public frmDanhSachDuTuyen()
        {
            InitializeComponent();
        }

        private void frmKhoa_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mTrinhDoVanHoa' table. You can move, or remove it, as needed.
            this.mTrinhDoVanHoaTableAdapter.Fill(this.qLTHDataSet.mTrinhDoVanHoa);
            // TODO: This line of code loads data into the 'qLTHDataSet.mTrinhDoDaoTao' table. You can move, or remove it, as needed.
            this.mTrinhDoDaoTaoTableAdapter.Fill(this.qLTHDataSet.mTrinhDoDaoTao);
            // TODO: This line of code loads data into the 'qLTHDataSet.mNghe' table. You can move, or remove it, as needed.
            this.mNgheTableAdapter.Fill(this.qLTHDataSet.mNghe);
            // TODO: This line of code loads data into the 'qLTHDataSet.mDoiTuongUuTien' table. You can move, or remove it, as needed.
            this.mDoiTuongUuTienTableAdapter.Fill(this.qLTHDataSet.mDoiTuongUuTien);
            // TODO: This line of code loads data into the 'qLTHDataSet.mDonViQuanDoi' table. You can move, or remove it, as needed.
            this.mDonViQuanDoiTableAdapter.Fill(this.qLTHDataSet.mDonViQuanDoi);
            // TODO: This line of code loads data into the 'qLTHDataSet.tDanhSachDuTuyen_getallwithdetail' table. You can move, or remove it, as needed.
            this.tDanhSachDuTuyen_getallwithdetailTableAdapter.Fill(this.qLTHDataSet.tDanhSachDuTuyen_getallwithdetail);
        }

        private void mniRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.tDanhSachDuTuyen_getallwithdetailTableAdapter.GetData();
                this.tDanhSachDuTuyen_getallwithdetailTableAdapter.Fill(this.qLTHDataSet.tDanhSachDuTuyen_getallwithdetail);
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                frmHocSinhDangKy objForm = new frmHocSinhDangKy(clsEnum.FORM_MODE.ADD);
                objForm.MdiParent = this.ParentForm;
                objForm.Show();
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                int intHSID;
                intHSID = clsCommon.fncCnvNullToInt(dgvDsDuTuyen.GetFocusedRowCellValue(colHS_ID));

                //nếu không có bản ghi được chọn -> thoát
                if (intHSID <= 0)
                {
                    clsCommon.fncMsgInfo("Hãy chọn một bản ghi để sửa.");
                    return;
                }

                //hiển thị form edit
                Cursor.Current = Cursors.WaitCursor;
                frmHocSinhDangKy objForm = new frmHocSinhDangKy(clsEnum.FORM_MODE.EDIT, intHSID);
                objForm.MdiParent = this.ParentForm;
                objForm.Show();
                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void mniDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                int intHSID;
                string strHoDem;
                string strTen;
                intHSID = clsCommon.fncCnvNullToInt(dgvDsDuTuyen.GetFocusedRowCellValue(colHS_ID));

                //nếu không có bản ghi được chọn -> thoát
                if (intHSID <= 0)
                {
                    clsCommon.fncMsgInfo("Hãy chọn một học sinh để xóa.");
                    return;
                }

                strHoDem = clsCommon.fncCnvNullToString(dgvDsDuTuyen.GetFocusedRowCellValue(colhs_hodem));
                strTen = clsCommon.fncCnvNullToString(dgvDsDuTuyen.GetFocusedRowCellValue(colhs_ten));
                if (!clsCommon.fncMsgConfirm(string.Format("Bạn có chắc muốn xóa thông tin học sinh {0} {1}?", strHoDem, strTen))) return;

                Cursor.Current = Cursors.WaitCursor;
                if (clsCommon.fncDeleteHs(intHSID)) dgvDsDuTuyen.DeleteSelectedRows();
                else clsCommon.fncMsgError("Xóa học sinh thất bại. Xin hãy thử lại.");

            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }
    }
}